﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.IO;
using CommonClasses;

namespace QCCM2
{
    public class Material
    {

        private int materialId;
        private string materialName;
        private string materialDescription;
        private bool materialDisabled;
        //Material aMaterial = new Material();
       

        public int MaterialID
        {
            get { return materialId; }
            set { materialId = value; }
        }


        public string MaterialName
        {
            get { return materialName; }
            set { materialName = value; }
        }

        public string MaterialDescription
        {
            get { return materialDescription; }
            set { materialDescription = value; }
        }

        public bool MaterialDisabled
        {
            get { return materialDisabled; }
            set { materialDisabled = value; }
        }

 


        public Material(string materialName)
        {
            //constructor that takes MaterialID as a parameter and returns MaterialName and MaterialDescription
            MaterialID = materialId;
            MaterialDescription = materialDescription;
            MaterialDisabled = materialDisabled;

        }

        public Material()
        {
            // TODO: Complete member initialization
        }

        Log LogUpdate = new Log();

        public List<Material> GetAllMaterials()
             
        {
            List<Material> MaterialList = new List<Material>();

            SqlConnection cs = new SqlConnection("Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
            string Query = "SELECT* FROM dbo.Material";
            SqlCommand cm = new SqlCommand(Query, cs);
            SqlDataReader dr;

            cs.Open();
            dr = cm.ExecuteReader();

            while (dr.Read())
            {
                Material ML = new Material();
                ML.MaterialID = (int)dr["MaterialID"];
                ML.MaterialName = (string)dr["MaterialName"];
                ML.MaterialDescription = (string)dr["MaterialDescription"];
                ML.MaterialDisabled = (bool)dr["MaterialDisabled"];
                MaterialList.Add(ML);

            }
            cs.Close();

            return MaterialList;
        }

        public string AddMaterial(string materialName, string materialDescription, bool materialDisabled)
        {
            string result = "";
            try
            {

                DataSet ds = new DataSet();
                SqlConnection cs = new SqlConnection("Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
                SqlDataAdapter da = new SqlDataAdapter();

                da.InsertCommand = new SqlCommand("INSERT INTO Material VALUES(@MaterialName, @MaterialDescription, @MaterialDisabled)", cs);
                da.InsertCommand.Parameters.Add("@MaterialName", SqlDbType.VarChar).Value = Convert.ToString(materialName);
                da.InsertCommand.Parameters.Add("@MaterialDescription", SqlDbType.VarChar).Value = Convert.ToString(materialDescription);
                da.InsertCommand.Parameters.Add("@MaterialDisabled", SqlDbType.Bit).Value = Convert.ToBoolean(materialDisabled);

                cs.Open();
                da.InsertCommand.ExecuteNonQuery();
                result = materialName + " has been Successfully Added";
                cs.Close();
            }

            catch (NullReferenceException exNull)
            {
                Console.WriteLine(exNull.Message);
                return materialName + "was not added. There was a problem";
            }

            //string Error = string.Empty;
            LogUpdate.LogTransaction(0, materialName + " has been Successfully Added", "", "Info");
            return materialName + " has been Successfully Added";

        }

        public string UpdateMaterial(string materialName, string materialDescription, bool materialDisabled)
        {
            string result = "";
            try
            {

                SqlConnection cs = new SqlConnection("Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
                string Query = "UPDATE dbo.Material SET MaterialName=@MaterialName, MaterialDescription=@MaterialDescription, MaterialDisabled=@MaterialDisabled WHERE MaterialName ='" + materialName + "'";
                SqlCommand cm = new SqlCommand(Query, cs);

                cs.Open();

                cm.Parameters.AddWithValue("@MaterialName", materialName);
                cm.Parameters.AddWithValue("@MaterialDescription", materialDescription);
                cm.Parameters.AddWithValue("@MaterialDisabled", materialDisabled);

                cm.ExecuteNonQuery();

                result = materialName + " has been updated.";


                cs.Close();
            }
            catch (System.Data.SqlClient.SqlException sqlException)
            {
                result = sqlException.Message;
            }
            catch (Exception ex)
            {
                result = "There was a problem. Your changes many not have been saved.\r" + ex.Message;
            }
            //string Error = string.Empty;
            LogUpdate.LogTransaction(0, materialName + " has been Successfully Updated", "", "Info");
            return result;
        }


        public string DeleteMaterial(string materialName)
        {
            string result = "";

            try
            {
                SqlConnection cs = new SqlConnection("Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
                string Query = "DELETE FROM dbo.Material where MaterialName = '" + materialName + "'";
                SqlCommand cm = new SqlCommand(Query, cs);

                cs.Open();
                cm.ExecuteNonQuery();
      
                result = materialName + " Successfully Deleted.";

                cs.Close();
            }

            catch (FileNotFoundException fnfe)
            {
                result = fnfe.Message;
            }
            catch (Exception ex)
            {
                result = "There was a problem. Your changes many not have been saved. " + ex.Message;
            }

            //string Error = string.Empty;
            LogUpdate.LogTransaction(0, materialName + " has been Successfully Deleted", "", "Info");
            return result;

        }


        public void DisableMaterial()
        {
            string result = "";
            try
            {
                SqlConnection cs = new SqlConnection("Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");

                string Query = "SELECT MaterialDisabled FROM dbo.Material WHERE MaterialName = '" + this.materialName + "'";
                SqlCommand cmdRead = new SqlCommand(Query, cs);
               
                cs.Open();
                SqlDataReader dr = cmdRead.ExecuteReader();

                materialDisabled = Convert.ToBoolean(dr["MaterialDisabled"]);

                result = materialName + " Successfully Deleted.";
                cs.Close();

            }

            catch (AccessViolationException)
            {
                throw new Exception("This material has NOT been disabled");
            }

            Console.WriteLine("This material has been disabled successfully.");

            //string Error = string.Empty;
            LogUpdate.LogTransaction(0, materialName + " has been Successfully Disabled", "", "Info");
            //return result;
        }


    }
}
